Scroll - cast some magic
_______________
()==( (@==()
'______________'|
| |
| ἀρετή |
__)_____________|
()==( (@==()
'--------------'
Documentation
Usage
Add to your Cargo.toml
[]
= "0.11"
Overview
Scroll implements several traits for read/writing generic containers (byte buffers are currently implemented by default). Most familiar will likely be the Pread
trait, which at its basic takes an immutable reference to self, an immutable offset to read at, (and a parsing context, more on that later), and then returns the deserialized value.
Because self is immutable, all reads can be performed in parallel and hence are trivially parallelizable.
A simple example demonstrates its flexibility:
use ;
Deriving Pread
and Pwrite
Scroll implements a custom derive that can provide Pread
and Pwrite
implementations for your structs.
use ;
This feature is not enabled by default, you must enable the derive
feature in Cargo.toml to use it:
[]
= { = "0.10", = ["derive"] }
std::io
API
Scroll can also read/write simple types from a std::io::Read
or std::io::Write
implementor. The built-in numeric types are taken care of for you. If you want to read a custom type, you need to implement the FromCtx
(how to parse) and SizeWith
(how big the parsed thing will be) traits. You must compile with default features. For example:
use Cursor;
use IOread;
Similarly, we can write to anything that implements std::io::Write
quite naturally:
use ;
use ;
Advanced Uses
Scroll is designed to be highly configurable - it allows you to implement various context (Ctx
) sensitive traits, which then grants the implementor automatic uses of the Pread
and/or Pwrite
traits.
For example, suppose we have a datatype and we want to specify how to parse or serialize this datatype out of some arbitrary byte buffer. In order to do this, we need to provide a TryFromCtx impl for our datatype.
In particular, if we do this for the [u8]
target, using the convention (usize, YourCtx)
, you will automatically get access to
calling pread_with::<YourDatatype>
on arrays of bytes.
use ;
// note the lifetime specified here
Please see the official documentation, or a simple example for more.
Contributing
Any ideas, thoughts, or contributions are welcome!